var browserVersion = $.browser.version;
/*
* 在地图上增加工具栏
*/
var addTool = function(htm,_width,_height,elemId){
	this.elemId = elemId==null?"#mapbar":"#"+elemId;
	this.index = 0;
	var width =_width==null?350:_width;
	this.isMaxed = false;
	var height = _height==null?300:_height;
	
	this.spanWidth = width;

	var prepareData = function(data){
			if(typeof data =="object"){
				var tmp = '<div id="tool_tabs" style="width:'+(width-8)+'px;height:'+(height-8)+'px;"><ul >';
				var z_index = 0;
				for(each in data){
					tmp += '<li class="tab_menu"><a href="#tabs-'+z_index+'" onclick="_tool_Index_id='+z_index+';">'+each+'</a></li>';
					z_index++;
				}
				tmp += '</ul>';
				z_index = 0;
				for(each in data){
					tmp += '<div id="tabs-'+z_index+'" >'+data[each]+'</div>';
					z_index++;
				}
				return tmp+'</div>';
		}else if(typeof data == 'string' && data.indexOf('URL:')>-1){
			return "<iframe src='"+data.substring(4)+"'  style='width:"+(width-1)+";height:"+(height-1)+";overflow:hidden;'></iframe>";
		}
			return null;
	}
	var _tmpHtml = prepareData(htm);
	var _position = $(this.elemId).position();
	_position.top = _position.top>0?_position.top:0;
	var disWinHeight = 0;

	var tmp = $(this.elemId).css('width');
	var m_width = tmp=='auto'?500:parseInt(tmp.replace('px',''));
	$(this.elemId).append('<div id="tool_div" style="background-color:#f4f5fa;position:relative;top:'+
		(_position.top+disWinHeight)+'px;left:'+(_position.left+m_width-width-4)+'px;width:'+width+'px;height:'+height+
		'px;overflow:hidden;border:2px solid #25569E;filter:alpha(opacity=80);z-index:100">'+
		(_tmpHtml==null?htm:_tmpHtml)+'</div>');
	$(this.elemId).append('<div id="tool_DIV_control_close" style="position:relative;top:'+(_position.top-17+disWinHeight)+'px;left:'+(_position.left+m_width-width-1)+
		'px;width:12px;height:12px;display:block;z-index:102;cursor:pointer;"><img id="controlToolBar" src="'+QDevelop_Application_Domain+'/lib/image/closeToolBar.gif" title="关闭工具栏" width="14" height="14"></div><div id="tool_DIV_control_open" style="position:relative;top:5px;left:'+(_position.left+m_width-26)+
		'px;width:30px;height:30px;display:block;z-index:10003;cursor:pointer;display:none"><img src="'+QDevelop_Application_Domain+'/lib/image/openToolBar.gif" title="打开工具栏" width="14" height="14"></div>');
		
		$('#tool_DIV_control_close').click(function(){
			$('#tool_div').hide('slow');
			$('#tool_DIV_control_close').hide();
			$('#tool_DIV_control_open').show();
		});
		$('#tool_DIV_control_open').mouseover(function(){
			$('#tool_DIV_control_open').hide();
			$('#tool_DIV_control_close').show();
			$('#tool_div').show('slow');
		});
		if(_tmpHtml!=null){
			$('#tool_tabs').tabs();
		}
}
var _tool_Index_id=0;//暂存当前tab Id
addTool.prototype.getIndex = function(){
	return _tool_Index_id;
}

addTool.prototype.reSetPosition = function(width){
	$('#tool_div').css({'left':(width-this.spanWidth-4)});
	$('#tool_DIV_control_open').css({'left':(width-20)});
	$('#tool_DIV_control_close').css({'left':(width-this.spanWidth-1)});
}

var createLayerControl = function(height){
	if(height!=null){
		return "<div style='height:"+height+"px;'></div><div class='barControl' onclick=layerControl(this);>隐藏叠加层</div>";
	}
	return "<div class='barControl'  onclick=layerControl(this);>隐藏叠加层</div>";
}
var layerControl = function(obj){
	if($(obj).html() == '隐藏叠加层'){
			$(obj).html('显示叠加层');
			removeLayer();
	}else{
			$(obj).html('隐藏叠加层');
			addLayer();
	}
}

 function removeLayer() {   
          if(item.layer)   
              maplet.removeLayer(item.layer,true);   
}  

var getImgHtml = function(src,htm,isMaxShow){
	htm = htm==null?'':htm;
	isMaxShow = isMaxShow==null?false:true;
	if(isIE())
		return '<div  class="reportImg">'+htm+'<IMG  onreadystatechange="showImgWithLoading(this)" style="display:none;'+(isMaxShow?"cursor:pointer;":"")+'" SRC="'+src+'&$r='+Math.random()+'" '+(isMaxShow?'id="img_Max_Show_Mark" title="单击大图显示"':'')+' border="0"><div><img src="'+QDevelop_Application_Domain+'/lib/image/loading.gif" width="200" height="200"><br><br>报表图片正在生成，请稍等...</div></div>';
	else
		return '<div  class="reportImg">'+htm+'<IMG  onload="showImgWithLoading(this)" style="display:none'+(isMaxShow?"cursor:pointer;":"")+'" SRC="'+src+'&$r='+Math.random()+'" '+(isMaxShow?'id="img_Max_Show_Mark" title="单击大图显示"':'')+' border="0"><div><img src="'+QDevelop_Application_Domain+'/lib/image/loading.gif" width="200" height="200"><br><br>报表图片正在生成，请稍等...</div></div>';
}

var reSetImgSrc = function(imgObj,src){
	$(imgObj).attr('src',src);
	$(imgObj).hide();
	$(imgObj).next().show();
}

var showImgWithLoading = function(obj){
	$(obj).show();
	$(obj).next().hide();
}
var isIE = function(){
	var appname = navigator.appName.toLowerCase();
    if (appname.indexOf("netscape") == -1)
		return true;
	else
		return false;
}


var odList = {'工作地分析':'ORIG_NUM','居住地分析':'DEST_NUM'};
var businessList = {'飞信':'feixin','彩铃':'cailing','手机报':'shoujibao','114':'114','12580':'12580','12593':'12593','手机邮件':'mobile_mail','艺龙':'elong','QQ':'qq','短信':'sms','携程':'ctrip','电话呼叫':'tel','12114':'12114'};
var odLast,businessLast;

var isClick = function(obj,type){
	
	if(marker!=null){
		marker.info.clearTabs();
		maplet.removeOverlay(marker);
	}

	if(type == 'OD'){
		if(odLast != null && odLast != obj){
			$(odLast).attr('class','toolButton');
		}else{
			$('#OD_toolsDiv > span').attr('class','toolButton');
		}
		odLast = obj;
	}else{
		if(businessLast != null && businessLast != obj){
			$(businessLast).attr('class','toolButton');
		}else{
				$('#business_toolsDiv > span').attr('class','toolButton');
		}
		businessLast = obj;
	}
	if(obj.className == 'toolButton'){
		$(obj).attr('class','toolButtonClick');
		return true;
	}else{
		$(obj).attr('class','toolButton');
	}
	return false;
}

var getLast2Day = function(){
	var date = new Date();
	var sD = new Date(date);
	var dD = new Date(date - 1*86400000);
	return sD.getFullYear()  + (sD.getMonth()<9?"0":"") +(sD.getMonth()+1) + (sD.getDate()<10?"0":"")+sD.getDate()
		+";"+dD.getFullYear()  + (dD.getMonth()<9?"0":"") +(dD.getMonth()+1) + (dD.getDate()<10?"0":"")+dD.getDate();
}

var getLast7Day = function(isSnap){
	var dis = (isSnap!=null&&isSnap)?0:2;
	var date = new Date();
	var sD = new Date(date - (7+dis)*86400000);
	var dD = new Date(date - dis*86400000);
	return sD.getFullYear() + "-" + (sD.getMonth()<9?"0":"") +(sD.getMonth()+1)+ "-" + (sD.getDate()<10?"0":"")+sD.getDate()
		+","+dD.getFullYear() + "-" + (dD.getMonth()<9?"0":"") +(dD.getMonth()+1)+ "-" + (dD.getDate()<10?"0":"")+dD.getDate();
}

var getLast14Day = function(isSnap){
	var dis = (isSnap!=null&&isSnap)?0:2;
	var date = new Date();
	var sD = new Date(date - (14+dis)*86400000);
	var dD = new Date(date - dis*86400000);
	return sD.getFullYear() + "-" + (sD.getMonth()<9?"0":"") +(sD.getMonth()+1)+ "-" + (sD.getDate()<10?"0":"")+sD.getDate()
		+","+dD.getFullYear() + "-" + (dD.getMonth()<9?"0":"") +(dD.getMonth()+1)+ "-" + (dD.getDate()<10?"0":"")+dD.getDate();
}


var _ImgMaxShow = new MoreInfoView({"title":"报表大图显示","width":640,"height":450,"isLeft":true});
//注册需要点击放大的图片
$('#img_Max_Show_Mark').live('click',function(){
	var _src = this.src;
	_src = _src.replace(/&width=[0-9]*/g,'').replace(/&height=[0-9]*/g,'')+"&width=570&height=340";
	var toShow = function(){
		_ImgMaxShow.show(getImgHtml(_src));	
	}
	window.setTimeout(toShow,100);//解决延时BUG
});

var moreInfoWin;
var toShowMoreInfo = function(_html){
	if(moreInfoWin==null)
		moreInfoWin = new MoreInfoView("最近7日数据统计报表",700,400);
	moreInfoWin.show(_html);
}